package com.google.BlueDiary

import android.annotation.SuppressLint
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import java.lang.invoke.MethodHandle
import android.util.Log
import android.content.Intent
import android.net.Uri


class MainActivity : FlutterActivity() {

    private val CHANNEL = "com.google.BlueDiary"
    private val method = "openBrowser"
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        val app = this

        MethodChannel(
            flutterEngine.dartExecutor.binaryMessenger,
            CHANNEL
        ).setMethodCallHandler { call, result ->
            when (call.method) {
                method -> {
               //    val map = call.arguments as Map<String, Any>
//                    val intent = Intent(app, PrivacyActivity::class.java)
//                    intent.putExtra("data", map["data"] as String?)
//                    startActivity(intent)

                    Log.d("app","call.arguments="+call.arguments())
                    val uri = Uri.parse(call.arguments())
                    val intent = Intent(Intent.ACTION_VIEW, uri)
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    try {
                        intent.setPackage("com.android.chrome") // 这一行设置了Chrome浏览器的包名
                        startActivity(intent)
                    } catch (e: Exception) {
                        e.printStackTrace()
                        startActivity(intent)
                    }
                }
            }
        }

    }

}
